package vn.edu.saigontech.ncms.web.system;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import vn.edu.saigontech.ncms.model.info.SchoolGrade;
import vn.edu.saigontech.ncms.service.SchoolGradeService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class SchoolGradeAction extends BaseAction {
	private Long id;
	private boolean active;
	private String title;
	
	public String list() {
		Collection<SchoolGrade> items = SchoolGradeService.INSTANCE.findAll();
		List<SchoolGrade> list = new ArrayList<SchoolGrade>(items);
		REQUEST().setAttribute("schoolGrades", list);
		return SUCCESS;
	}
	
	public String showAdd() {
		return SUCCESS;
	}
	
	public String add() {
		SchoolGrade item = new SchoolGrade();
		item.setTitle(this.title);
		item.setActive(true);
		item = SchoolGradeService.INSTANCE.persist(item);
		if(Checker.isNotNull(item)) {
			this.addLoggingDetail(BaseAction.ADD, "logging.schoolGrade.add", item.getTitle());
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String delete() {
		SchoolGrade item = SchoolGradeService.INSTANCE.findById(this.id);
		String title = item.getTitle();
		if(SchoolGradeService.INSTANCE.deleteById(this.id)) {
			this.addLoggingDetail(BaseAction.DELETE, "logging.schoolGrade.delete", title);
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String showEdit() {
		SchoolGrade item = SchoolGradeService.INSTANCE.findById(this.id);
		REQUEST().setAttribute("item", item);
		return SUCCESS;
	}
	
	public String edit() {
		SchoolGrade item = SchoolGradeService.INSTANCE.findById(this.id);
		item.setTitle(this.title);
		item.setActive(this.active);
		item = SchoolGradeService.INSTANCE.persist(item);
		if(Checker.isNotNull(item)) {
			this.addLoggingDetail(BaseAction.EDIT, "logging.schoolGrade.edit", item.getTitle());
			return SUCCESS;
		}
		return ERROR;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}
}